



//
//  AnimationGroup.m
//  成品家商城
//
//  Created by YYY on 2017/6/26.
//  Copyright © 2017年 成品家（北京）网络科技有限公司. All rights reserved.
//

#import "AnimationGroup.h"

/*
 初始化气泡视图：bubbleView.transform = CGAffineTransformMakeScale(0, 0);
 */
//是否显示
static BOOL isShow;

@implementation AnimationGroup

SingletonM;

//抖动动画
-(void)shakeAnimation:(UIView *)targetObject
{
    static CABasicAnimation *animation;
    if (animation == nil)
    {
        animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        //动画持续时间
        animation.duration = 0.2;
        //抖动角度 1/180
        animation.fromValue = @(-M_1_PI/2);
        animation.toValue = @(M_1_PI/2);
        //重复次数
        animation.repeatCount = 2;
        //结束后执行逆动画
        animation.autoreverses = YES;
        //锚点设置为图片中心，绕中心抖动
        targetObject.layer.anchorPoint = CGPointMake(0.5, 0.5);
        
    }
    [targetObject.layer addAnimation:animation forKey:@"rotation"];
}
//缩放关键帧动画
-(void)scaleKeyframeAnimation:(UIView *)targetObject
{
    //关键帧动画
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    animation.values = @[@1.2,@0.8,@1.0];
    animation.duration = 0.8f;
    animation.calculationMode = kCAAnimationCubic;
    //添加动画
    [targetObject.layer addAnimation:animation forKey:@"scale"];
}

//弹出气泡视图
-(void)showBubbleViewAnimation:(UIView *)bubbleView
{
    if (isShow)
    {
        //隐藏
        [UIView animateWithDuration:1 animations:^{
            bubbleView.transform = CGAffineTransformMakeScale(0.1, 0.1);
            bubbleView.alpha = 0;
        }];
        isShow = NO;
    }
    else
    {
        //显示
        [UIView animateWithDuration:1 animations:^{
            bubbleView.transform = CGAffineTransformMakeScale(1, 1);
            bubbleView.alpha = 1;
        }];
        isShow = YES;
    }
}

//缩放动画
-(void)scaleAnimation:(UIView *)targetObject
{
    [UIView animateWithDuration:1.0f animations:^{
        [UIView animateWithDuration:0.33 animations:^{
            [targetObject setTransform:CGAffineTransformMakeScale(1.2, 1.2)];
        }];
    } completion:^(BOOL finished)
     {
         [UIView animateWithDuration:0.33 animations:^{
             [targetObject setTransform:CGAffineTransformIdentity];
         }];
     }];
}

//动画1
- (CATransform3D)firstTransform
{
    CATransform3D t1 = CATransform3DIdentity;
    t1.m34 = 1.0/900;
    //带点缩小的效果
    t1 = CATransform3DScale(t1, 0.95, 0.95, 1);
    //绕x轴旋转
    t1 = CATransform3DRotate(t1, -10 * M_PI/180.0, 1, 0, 0);
    return t1;
}
//动画2
- (CATransform3D)secondTransform
{
    CATransform3D t2 = CATransform3DIdentity;
    t2.m34 = [self firstTransform].m34;
    //向上移
    t2 = CATransform3DTranslate(t2, 0, kDeviceHeight * (-0.08), 0);
    //第二次缩小
    t2 = CATransform3DScale(t2, 0.8, 0.8, 1);
    return t2;
}

@end
